<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title>用户修改</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link rel="stylesheet" href="/layui/css/layui.css" media="all" />
    <link rel="stylesheet" href="/css/user.css" media="all" />
    <link rel="stylesheet" type="text/css" href="//at.alicdn.com/t/font_1041315_0zq2yro0cfn.css">
    <style type="text/css">
        .layui-form-item .layui-inline{ width:33.333%; float:left; margin-right:0; }
        @media(max-width:1240px){
            .layui-form-item .layui-inline{ width:100%; float:none; }
        }
        .layui-form-item .role-box {
            position: relative;
        }
        .layui-form-item .role-box .jq-role-inline {
            height: 100%;
            overflow: auto;
        }

    </style>
</head>
<body class="childrenBody">
<form class="layui-form" style="width:80%;">
    <div class="user_left">
    <input class="layui-hide" name="id" th:value="${user.id}"/>
    <div class="layui-form-item">
        <label class="layui-form-label">登录名</label>
        <div class="layui-input-block">
            <input type="text" class="layui-input" name="userName" lay-verify="required" placeholder="请输入登录名" th:value="${user.userName}">
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">用户名</label>
            <div class="layui-input-block">
                <input type="text" class="layui-input" name="realName" placeholder="请输入用户名" th:value="${user.realName}">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">邮箱</label>
            <div class="layui-input-block">
                <input type="text" class="layui-input" name="email" lay-verify="email" placeholder="请输入邮箱" th:value="${user.email}">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">手机</label>
            <div class="layui-input-block">
                <input type="text" class="layui-input" name="phone" lay-verify="phone" placeholder="请输入手机号" th:value="${user.phone}">
            </div>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">用户角色</label>
        <div class="layui-input-block role-box">
            <div class="jq-role-inline">
                <fieldset class="layui-elem-field" th:if="${roleList!=''}">
                    <legend>选择角色</legend>
                    <div class="layui-field-box" >
                                <input type="checkbox" name="roles" th:each="role:${roleList}" th:value="${role.id}"
                                       th:text="${role.roleName}" lay-filter="role" th:checked="${#lists.contains(roleIdList,role.id)}"/>
                    </div>
                </fieldset>
            </div>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">是否启用</label>
        <div class="layui-input-block">
            <input type="checkbox" name="locked" lay-skin="switch" value="true"  lay-text="启用|锁定"th:checked="${user.locked==0 }">
        </div>
    </div>
    </div>
    <div class="layui-col-md3 layui-col-xs12 user_right">
        <input type="hidden"  name="image"  th:value="${user.image}" >
        <div class="layui-upload-list">
            <img    th:src="${user.image}"class="layui-circle"  alt="图片"  id="userFace">
        </div>
        <button type="button" class="layui-btn layui-btn-primary userFaceBtn" id="userFaceBtn"><i class="layui-icon">&#xe67c;</i> 换个头像</button>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit="" lay-filter="addUser"><i class="iconfont icon-xiugai1"></i>修改</button>
            <button class="layui-btn layui-btn-normal" id="close" ><i class="iconfont icon-quxiao"></i>取消</button>
        </div>
    </div>
</form>
<script type="text/javascript" src="/layui/layui.js"></script>
<script type="text/javascript" src="/js/common/address.js"></script>
<script type="text/javascript" src="/js/user/user.js"></script>
<script>
    var index = parent.layer.getFrameIndex(window.name); //当前窗口索引
    layui.config({
        base: '/js/common/' //假设这是你存放拓展模块的根目录
    }).use(['form','jquery','layer','common'],function(){
        var form = layui.form,
                $    = layui.jquery,
            common=layui.common,
                layer = layui.layer;

        form.on("submit(addUser)",function(data){
            if(data.field.id == null){
                layer.msg("用户ID不存在");
                return false;
            }
            // //给角色赋值
            // delete data.field["roles"];
            // var selectRole = [];
            // $('input[name="roles"]:checked').each(function(){
            //     selectRole.push({"id":$(this).val()});
            // });
            var r=document.getElementsByName("roles");
            var role=[];
            for(var i=0;i<r.length;i++){
                if(r[i].checked){
                    console.info(r[i].value);
                    role.push(r[i].value);
                }
            }
            data.field.roles = role;
            //判断用户是否启用
            if(undefined !== data.field.locked && null != data.field.locked){
                data.field.locked = 0;
            }else{
                data.field.locked = 1;
            }
            var loadIndex = layer.load(2, {
                shade: [0.3, '#333']
            });
            var userSaveLoading = top.layer.msg('数据提交中，请稍候',{icon: 16,time:500,shade:0.8});

            $.post("edit",data.field,function (res) {
                    top.layer.close(userSaveLoading);
                    if(res.success){
                    common.cmsLaySucMsg(res.message);
                    var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                    parent.layer.close(index); //再执行关闭                        //刷新父页面
                    parent.location.reload();
                    }else{
                    top.layer.close(userSaveLoading);
                     common.cmsLayErrorMsg(res.message);
                     parent.layer.close(index); //再执行关闭
                    }

            });
            return false;
        });
        $('#close').click(function(){
            var index = parent.layer.getFrameIndex(window.name);
            parent.layer.close(index);
        });
    });
</script>
</body>
</html>